package com.ksc.wordcount.test;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;

import java.io.File;
import java.io.IOException;


public class ReadAvro {

    public static void main(String[] args) throws IOException {
//        File avroFile = new File("D:/kingsoft/source/TopN/wordcountdemo2/tmp/output/application.avro");
//
//        // 1. 创建Avro模式
//        Schema avroSchema = new Schema.Parser().parse(new File("src/main/java/com/ksc/wordcount/avro/urlTopN.avsc"));
//
//        // 2. 创建Avro解码器
//        DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(avroSchema);
//        DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(avroFile, datumReader);
//
//        // 3. 读取Avro文件中的数据
//        while (dataFileReader.hasNext()) {
//            GenericRecord avroData = dataFileReader.next();
//            String key = avroData.get("url").toString();
//            int value = (int) avroData.get("count");
//            // 在这里进行你的操作，例如打印或处理数据
//            System.out.println("Key: " + key + ", Value: " + value);
//        }
//
//        dataFileReader.close();

        readOutputAvro();
    }
    public static void readOutputAvro() {
        String avroFilePath = "D:/kingsoft/source/TopN/wordcountdemo2/tmp/output/application_1234.avro"; // 修改为实际的文件路径

        try {
            File avroFile = new File(avroFilePath);
            DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
            DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(avroFile, datumReader);

            Schema schema = dataFileReader.getSchema();
            System.out.println("Avro Schema: " + schema);

            for (GenericRecord record : dataFileReader) {
                String key = record.get("key").toString();
                int value = (int) record.get("value");
                System.out.println("Key: " + key + ", Value: " + value);
            }

            dataFileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
